package com.jie.deletefile.ExpandableListView;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jie.deletefile.R;

import java.util.ArrayList;
import java.util.List;

public class ExlistviewActivity extends AppCompatActivity {

    private ExpandableListView elv;
    private RelativeLayout activity_exlistview;
    private List<String> datap, datas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exlistview);
        initView();
    }

    private void initView() {
        elv = (ExpandableListView) findViewById(R.id.elv);
        activity_exlistview = (RelativeLayout) findViewById(R.id.activity_exlistview);
        datap = new ArrayList<>();
        datas = new ArrayList<>();
        for (int i=0;i<10;i++) {
            String s = "第一层" + i + " ";
            datap.add(s);
        }
        for (int j = 0; j < 5; j++) {
            String ss = "第二层" + j + "  ";
            datas.add(ss);
        }
        ExAdapter adapter = new ExAdapter(this, datap, datas);
        elv.setGroupIndicator(null);
        elv.setAdapter(adapter);

    }

    class ExAdapter extends BaseExpandableListAdapter {

        private Context context;
        private List<String> datap;
        private List<String> datas;

        public ExAdapter(Context context, List<String> datap, List<String> datas) {
            this.context = context;
            this.datap = datap;
            this.datas = datas;
        }

        @Override
        public int getGroupCount() {
            return datap.size();
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return datas.size();
        }

        @Override
        public Object getGroup(int groupPosition) {
            return datap.get(groupPosition);
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return datas.get(childPosition);
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Override
        public boolean hasStableIds() {
            return false;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.ex_p_item, parent, false);
            }
            TextView textView = (TextView) convertView.findViewById(R.id.elv_p_tv);
            textView.setText(datap.get(groupPosition));
            return convertView;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.ex_s_item,parent,false);

            }
            TextView textView = (TextView) convertView.findViewById(R.id.ex_s_tv);
            textView.setText(datas.get(childPosition));
            return convertView;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return false;
        }
    }

}
